package com.bdq.interaction.api.controller;

import cn.hutool.core.convert.Convert;
import com.bdq.common.entity.common.CommonResult;
import com.bdq.common.entity.common.PageResult;
import com.bdq.interaction.api.model.param.*;
import com.bdq.interaction.api.model.vo.ChildCommentPageVO;
import com.bdq.interaction.api.model.vo.ParentCommentPageVO;
import com.bdq.interaction.api.model.vo.SignUpUserPageVO;
import com.bdq.interaction.application.model.cmd.*;
import com.bdq.interaction.application.model.dto.ChildCommentPageAppDTO;
import com.bdq.interaction.application.model.dto.ParentCommentPageAppDTO;
import com.bdq.interaction.application.model.dto.SignUpUserPageAppDTO;
import com.bdq.interaction.application.model.query.ChildCommentPageQry;
import com.bdq.interaction.application.model.query.ParentCommentPageQry;
import com.bdq.interaction.application.model.query.SignUpUserPageQry;
import com.bdq.interaction.application.service.InteractionAppCmdService;
import com.bdq.interaction.application.service.InteractionAppQueryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * bdq-interaction交互相关
 *
 * @author malei
 * @date 2024-05-10 11:13
 */
@RestController
@RequestMapping("/interaction")
public class InteractionController {

    @Resource
    private InteractionAppCmdService interactionAppCmdService;

    @Resource
    private InteractionAppQueryService interactionAppQueryService;

    /**
     * 浏览
     *
     * @return
     */
    @PostMapping("/view")
    public CommonResult<Boolean> view(@RequestBody @Validated ViewParam param) {
        interactionAppCmdService.view(Convert.convert(ViewCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 点赞
     *
     * @return
     */
    @PostMapping("/like")
    public CommonResult<Boolean> like(@RequestBody @Validated LikeParam param) {
        interactionAppCmdService.like(Convert.convert(LikeCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 取消点赞
     *
     * @return
     */
    @PostMapping("/like/cancel")
    public CommonResult<Boolean> cancelLike(@RequestBody @Validated LikeCancelParam param) {
        interactionAppCmdService.cancelLike(Convert.convert(LikeCancelCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 收藏
     *
     * @return
     */
    @PostMapping("/collect")
    public CommonResult<Boolean> collect(@RequestBody @Validated CollectParam param) {
        interactionAppCmdService.collect(Convert.convert(CollectCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 取消收藏
     *
     * @return
     */
    @PostMapping("/collect/cancel")
    public CommonResult<Boolean> cancelCollect(@RequestBody @Validated CollectCancelParam param) {
        interactionAppCmdService.cancelCollect(Convert.convert(CollectCancelCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 报名
     *
     * @return
     */
    @PostMapping("/sign-up")
    public CommonResult<Boolean> signUp(@RequestBody @Validated SignUpParam param){
        interactionAppCmdService.signUp(Convert.convert(SignUpCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 报名
     *
     * @return
     */
    @GetMapping("/sign-up/user/page")
    public CommonResult<PageResult<SignUpUserPageVO>> pageSingUpUser(@Validated SignUpUserPageParam param){
        PageResult<SignUpUserPageAppDTO> page = interactionAppQueryService.pageSingUpUser(Convert.convert(SignUpUserPageQry.class, param));
        PageResult<SignUpUserPageVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(Convert.toList(SignUpUserPageVO.class, page.getList()));
        return CommonResult.success(result);
    }

    /**
     * 投票
     *
     * @param param
     * @return
     */
    @PostMapping("/vote")
    public CommonResult<Boolean> vote(@RequestBody @Validated VoteParam param) {
        interactionAppCmdService.vote(Convert.convert(VoteCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 评论
     *
     * @param param
     * @return
     */
    @PostMapping("/comment")
    public CommonResult<Boolean> comment(@RequestBody @Validated CommentParam param) {
        interactionAppCmdService.comment(Convert.convert(CommentCmd.class, param));
        return CommonResult.success(true);
    }

    /**
     * 分页查询父评论
     *
     * @param param
     * @return
     */
    @GetMapping("/comment/parent/page")
    public CommonResult<PageResult<ParentCommentPageVO>> pageParentComment(@Validated ParentCommentPageParam param) {
        PageResult<ParentCommentPageAppDTO> page = interactionAppQueryService.pageParentComment(Convert.convert(ParentCommentPageQry.class, param));
        PageResult<ParentCommentPageVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(Convert.toList(ParentCommentPageVO.class, page.getList()));
        return CommonResult.success(result);
    }

    /**
     * 分页查询子评论
     *
     * @param param
     * @return
     */
    @GetMapping("/comment/child/page")
    public CommonResult<PageResult<ChildCommentPageVO>> pageParentComment(@Validated ChildCommentPageParam param) {
        PageResult<ChildCommentPageAppDTO> page = interactionAppQueryService.pageChildComment(Convert.convert(ChildCommentPageQry.class, param));
        PageResult<ChildCommentPageVO> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setList(Convert.toList(ChildCommentPageVO.class, page.getList()));
        return CommonResult.success(result);
    }
}
